package com.movecar.ui;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.movecar.R;
import com.movecar.model.Pictures;
import com.movecar.utils.AppConfig;
import com.movecar.utils.imageloader.Constants;
import com.movecar.widget.HackyViewPager;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 俱乐部图片
 * 
 * @author Zhao.JQ
 * 
 */
public class ClubPhotoActivity extends Activity implements OnClickListener {
	private HackyViewPager mViewPager;
	private PhotoView mImageView;
	public ImageLoader imageLoader;
	private int currentId = 0;
	private int maxId = 0;
	private List<PhotoView> mList = null;
	public static int IMAGESHOW = 100;
	private ArrayList<Pictures> picturesData = null;
	private MyPagerAdapter adapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.club_photo);
		initView();
	}

	@SuppressWarnings("unchecked")
	private void initView() {
		imageLoader = ImageLoader.getInstance();
		// imageLoader.init(Constants.imageLoaderConfiguration(this));
		Button btn_left = (Button) findViewById(R.id.btn_left);
		btn_left.setVisibility(View.VISIBLE);
		btn_left.setOnClickListener(this);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("图片");

		mViewPager = (HackyViewPager) findViewById(R.id.viewFlipper);

		if (this.getIntent() != null) {
			picturesData = (ArrayList<Pictures>) getIntent().getSerializableExtra("Pictures");
			currentId = getIntent().getIntExtra("currentId", 0);
			mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
			setData();

		}

	}

	private void setData() {

		mList = new ArrayList<PhotoView>();
		if (picturesData.size() > 0) {
			maxId = picturesData.size();
			for (int i = 0; i < maxId; i++) {
				mImageView = new PhotoView(this);
				mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				//mImageView.setScaleType(ScaleType.FIT_CENTER);
				mList.add(mImageView);

			}
			if (picturesData.size() == 1 || currentId == 0) {
				imageLoader.displayImage(AppConfig.HOST + picturesData.get(0).getUrl(), mList.get(0),
						Constants.image_display_options, new Constants.AnimateFirstDisplayListener());
			}
			adapter = new MyPagerAdapter(mList);

			mViewPager.setAdapter(adapter);
			mViewPager.setCurrentItem(currentId);

		}

	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter {
		public List<PhotoView> mListViews;

		public MyPagerAdapter(List<PhotoView> mListViews) {
			this.mListViews = mListViews;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1), 0);

			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_left:
			finish();
			break;

		default:
			break;
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onResume() {
		super.onResume();

	}

	public void onPause() {
		super.onPause();
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		public void onPageSelected(int arg0) {
			Log.e("---------------------->>>>>>>page", String.valueOf(arg0));

			imageLoader.displayImage(AppConfig.HOST + picturesData.get(arg0).getUrl(), mList.get(arg0),
					Constants.image_display_options, new Constants.AnimateFirstDisplayListener());

		}
	}

}
